package com.daytask.controller;

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.daytask.formbean.DaytaskFinish;
import com.daytask.formbean.SonTasktype;
import com.daytask.util.ControllorUtils;
import com.daytask.util.DateUtils;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;

public class DaytaskFinishController extends Controller {
	// id daytaskid daytaskname finsihquantum finishday createtime
	public Map<String, Object> responseM = new HashMap<String, Object>();// 用来存放返回的数据（json）

 
	public void add() {
		boolean saveResult = false;
		DaytaskFinish form = getModel(DaytaskFinish.class);
		if ( form.getInt("sontasktypeid")>0 && form.getDouble("finishnum") > 0 && form.getTimestamp("finishday") != null) {
			SonTasktype sonTasktype = SonTasktype.dao.findById(form.getInt("sontasktypeid"));
			if (sonTasktype != null) {
				Calendar cal=Calendar.getInstance(); 
				cal.setTimeInMillis( form.getTimestamp("finishday").getTime() );
				 form.set("finishyear",  cal.get(Calendar.YEAR));
				 form.set("finishweek",  	cal.get(Calendar.WEEK_OF_YEAR));
				 form.set("finishmonth",  cal.get(Calendar.MONTH)+1);
				form.set("sontasktypeid", sonTasktype.get("id"));
				form.set("sontasktypename", sonTasktype.get("name"));
				form.set("userid", ControllorUtils.getUserid());
				form.set("createtime", new Timestamp(System.currentTimeMillis()));
				saveResult = form.save();
			}
		}
		if (saveResult == true) {
			ControllorUtils.createJsonResultMap(responseM, 1);
		} else {
			ControllorUtils.createJsonResultMap(responseM, 0);
		}
		renderJson(responseM);
	}
	
	public void countFinish(String userId){
	String today =  DateUtils.formatDate(new Date(), "yyyy-MM-dd");
	String end = DateUtils.formatDate(DateUtils.addDay(new Date(), 1), "yyyy-MM-dd 00:00:00");
		//cal.add(field, amount);
		String daysql = "select  * from tb_daytask_finish where userid='"+userId+"'  and  DATE(finishday)=DATE_FORMAT('"+today+"','%Y%m%d')" ;
		List<DaytaskFinish> daylist = DaytaskFinish.dao.find(daysql);
	 
		String montsql = "SELECT * FROM tb_daytask_finish WHERE DATE_FORMAT(finishday, '%Y%m') = DATE_FORMAT('"+today+"' , '%Y%m')";
		List<DaytaskFinish> monthList = DaytaskFinish.dao.find(montsql);
		
		
	}
	public static void main(String[] args) {
		DaytaskFinishController d = new DaytaskFinishController();
		d.countFinish("liquan");
	}

	public void delete() {
		String deleteIds = getPara("deleteIds");
		int deleteRecord = 0;
		if (StringUtils.isNotBlank(deleteIds)) {
			String ids[] = StringUtils.split(deleteIds, ",");
			for (String id : ids) {
					if (DaytaskFinish.dao.deleteById(id) == true)
						deleteRecord++;
			}
			if (deleteRecord == ids.length) {
				ControllorUtils.createJsonResultMap(responseM, 1);
			} else if (deleteRecord > 0 && deleteRecord < ids.length) {
				ControllorUtils.createJsonResultMap(responseM, 2);// 部分删除成功
			} else {
				ControllorUtils.createJsonResultMap(responseM, 0); // 删除失败
			}
		} else {
			ControllorUtils.createJsonResultMap(responseM, 0); // 删除失败

		}
		renderJson(responseM);
	}

	public void list() {
		int page =Integer.parseInt( getPara("page"));
		int rows = Integer.parseInt(getPara("rows"));
		page=page<1?1:page;
		String select  ="select  * ";
		String fromSql ="from tb_daytask_finish   ";
		Page<DaytaskFinish> list = DaytaskFinish.dao.paginate(page,rows, select , fromSql);
		 responseM.put("total", list.getTotalRow());
		 responseM.put("rows",list.getList());
		  renderJson(responseM);
	}

	public void edit() {
		boolean  result = false;
		DaytaskFinish form = getModel(DaytaskFinish.class);
		int id = form.getInt("id");
		if (form.getInt("id")>0 && form.getDouble("finishnum") > 0) {
			DaytaskFinish dbObj = form.findById(id);
			if (dbObj != null) {
				dbObj.set("finishnum", form.getDouble("finishnum"));
				dbObj.set("finishday", form.getTimestamp("finishday"));
				result = dbObj.update( );
			}
		}
		if (result == true) {
			ControllorUtils.createJsonResultMap(responseM, 1);
		} else {
			ControllorUtils.createJsonResultMap(responseM, 0);
		}
		renderJson(responseM);

	}

}
